const {
  addNews,
  addONews,
  getNewsList,
  delNews,
  updateNews,
} = require("../controller/news");
const { SuccessModel, ErrorModel } = require("../model/resModel");
var express = require("express");
var router = express.Router();

const jwt = require("jsonwebtoken");
const loginCheck = require("../middleware/loginCheck");
const authCheck = require("../middleware/authCheck");
/* GET users listing. */

router.post("/add_news", (req, res, next) => {
  // console.log(req.body);
  const result = addNews(req.body);
  return result.then((val) => {
    if (val) {
      res.json(new SuccessModel());
    } else {
      res.json(new ErrorModel("添加失败"));
    }
  });
});

router.get("/get_news", function (req, res, next) {
  const result = getNewsList();
  return result.then((val) => {
    res.json(new SuccessModel(val));
  });
});
router.get("/del_news", (req, res, next) => {
  const result = delNews(req.query.id);
  // console.log(req.query.id);
  return result.then((val) => {
    if (val) {
      res.json(new SuccessModel(val));
    } else {
      res.json(new ErrorModel("删除失败"));
    }
  });
});
router.post("/update_news", function (req, res, next) {
  console.log(req.query.id, req.body);
  const result = updateNews(req.query.id, req.body);
  return result.then((val) => {
    if (val) {
      res.json(new SuccessModel());
    } else {
      res.json(new ErrorModel("更新失败"));
    }
  });
});
module.exports = router;
